<?php 

	class Msg
	{
		private $list;
		private $db;

		function __construct ()
		{
			include_once('../db.php');
			$this->db = new DB();
		}

		/**
		 * [显示留言列表]
		 * @param  [int] $page [第几页]
		 * @return [type]       [查询的分页的结果]
		 */
		function listRow($page,$pagesize,$option,$url)
		{
			if(empty($option)){
				$option = true;
			}
			//获得留言的总数
			$sql1 = "select * from article where $option";
			$count = $this->db->getCount($sql1);
			//计算分页数
			$count = ceil($count/$pagesize);
			//查询第page页的内容
			$pagecount = ($page-1)*$pagesize;
			$sql = "select * from article where $option limit $pagecount,$pagesize";
			$this->list = $this->db->get($sql);

			//显示留言结果 
 			for ($i=1; $i <= count($this->list); $i++) {
 			    $id = $this->list[$i-1][0];
 				$s = $this->list[$i-1][4];
 				$str=htmlspecialchars_decode($s,ENT_QUOTES); 
 				echo "<li>文章内容$i:$str
 				<a href='list.php?act=update&id=$id'>修改</a> 
 				<a href='list.php?act=del&id=$id'>删除</a></li>";
 			} 
 			//显示分页
 			echo "共{$count}页 当前是第{$page}页 ";

 			for($i=1;$i<=$count;$i++){
				if($page == $i){
					echo $i;
				}else{			
					echo "<a href='$url?page=$i'>[$i]</a>";
				}
			}
		}
		/**
		 * 留言功能
		 * @return [type] [description]
		 */
		function add($arr,$id)
		{
			$id = $id?$id:0;
            $title=$arr['title']; 
            $menu=$arr['menu'];
            $status=$arr['status'];
            $content=htmlspecialchars($arr['content'],ENT_QUOTES,'utf-8');
            $create_time=$arr['create_time'];
			$sql = "insert into article values
			('$id','$title','$menu','$status','$content','$create_time')";
			if($this->db->insert($sql)){
				echo "添加成功";
				header("loaction:cate_manage.php");
			}
		}

		function delMsg($id)
		{
			$sql = "delete from article where id= $id";
			if($this->db->delete($sql)){
				echo "删除成功";
			}
		}

		function getMessage($id)
		{
			$sql = "select * from article where id = $id";
			$s= $this->db->get($sql);
			$res=htmlspecialchars_decode($s,ENT_QUOTES);
			return $res;
		}

		function updateMsg($data)
		{
			$id =$data['id'];
			$title=$data['title'];
			$menu =$data['menu'];
			$content =$data['content'];
			$status=$data['status'];
			$time=$data['time'];
			$sql = "update article set id='$id',title='$title',status='$status',menu='$menu',content ='$content',create_time='$time' where id='$id'";
			if($this->db->update($sql)){
				return true;
			}else{
				return false;
			}
		}
	}


 ?>